package com.base;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.io.FileUtils;

import com.data.Data;
import com.view.P3;

public class dowImg extends Thread
{
    private String url;
    private File file;
    public dowImg(String url,File file)
    {
        this.url = url;
        this.file = file;
    }
    public void run()
    {  
    	try {
    		System.out.println(url);
    		url = toString20(url);
			FileUtils.copyURLToFile(new URL(url), file);
			if(Data.imgUrl.equals(file.getPath())) {
				P3.instance.setImgBoxData(file.getPath());
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    public String toString20(String url) {
    	//如果文件路径有空格会进行转换
    	String url1="";
		try {
			url1 = URLEncoder.encode(url, "utf-8").replaceAll("\\+", "%20");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	String url2 = url1.replaceAll("%3A", ":").replaceAll("%2F", "/");
    	return url2;
    }
    public static void main(String[] args) throws MalformedURLException, IOException {
		
	}
}